//
//  ShellSysMessageViewModel.m
//  VVBTC
//
//  Created by lvzhao on 2019/7/11.
//  Copyright © 2019  吕VV. All rights reserved.
//

#import "ShellSysMessageViewModel.h"

@interface ShellSysMessageViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end
@implementation ShellSysMessageViewModel
/**
 Convert 站内信
 @param  isFirst 是否是第一次
 */
- (void)getUserMessage:(BOOL)isFirst{
    
    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kMessageQueryMsgList,(long)self.pageIndex];
    @weakify(self);
    [LZNetworkingManager  lz_request:@"get" url:url params:nil success:^(id  _Nullable responseObject) {
        @strongify(self);
        dismiss(nil);
        if([responseObject[kResultStatus] isEqualToString:kCode]){

            NSArray *dataArray = responseObject[kResultData];
              
              for (int i = 0; i < dataArray.count; i++) {
                  ShellSysMessageModel *messageModel = [ShellSysMessageModel mj_objectWithKeyValues:dataArray[i]];
                  [self.dataArray addObject:messageModel];
              }
        }
      
        //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
        NSInteger totalCount = [responseObject[@"total"] integerValue];
        if(self.dataArray.count == totalCount){
            //没有更多数据
            [self.reloadSubject sendNext:@(0)];
            return;
        }
        //看是否有数据
        if(self.dataArray.count == 0){
            [self.reloadSubject sendNext:@(-1)];
        } else {
            self.pageIndex++;
            [self.reloadSubject sendNext:@(1)];
        }
        
    } failure:^(NSError * _Nullable error) {
        
    } notwork:^(NSError * _Nullable error) {
        
    } isLoading:NO isFailTip:YES];
}

/**
 Convert 标记是否已读
 @param messageModel model
 */
- (void)updateMessageStatus:(ShellSysMessageModel *)messageModel{
    
    if([messageModel.readFlag isEqualToString:@"Y"])return;
    
    NSDictionary *params = @{@"msgId":@(messageModel.messageId)};
    
    @weakify(self);
    [LZNetworkingManager lz_request:@"post" url:kReadMsg params:params success:^(id  _Nullable responseObject) {
        @strongify(self);
        dismiss(LZCurrentView);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            messageModel.readFlag = @"Y";
            [self.reloadSubject sendNext:@(1)];
        }
    } failure:^(NSError * _Nullable error) {
        
    } isFailTip:YES];
}

@end
